//
//  CFDetailViewController.m
//  CFOnlineShop
//
//  Created by chenfeng on 2018/8/6.
//  Copyright © 2018年 chenfeng. All rights reserved.
//

#import "CFDetailViewController.h"
//#import "CFDetailView.h"
#import "SDCycleScrollView.h"
#import "ProductDetailTableViewCell.h"
#import "ProductCommentTableViewCell.h"
@interface CFDetailViewController ()<UITableViewDelegate,UITableViewDataSource>


@property (nonatomic, strong) UIWebView *webView;


//添加在头部视图的tempScrollView
@property (nonatomic, strong) UIScrollView *tempScrollView;
//记录底部空间所需的高度
@property (nonatomic, assign) CGFloat bottomHeight;

//@property (nonatomic, strong) CFDetailView *detailView;

@end

@implementation CFDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setBgUI];
    [self setHeaderAndFooterView];
    [self setBottomView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)dealloc
{
    _webView.delegate = nil;
    _webView = nil;
    NSLog(@"释放View");
}

- (void)setBgUI
{
    _bottomHeight = 40;
    
    //存放tableView和webView，tableview在上面，webview在下面
    _bigView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, (Main_Screen_Height - _bottomHeight) * 2)];
    _bigView.backgroundColor = kWhiteColor;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height - _bottomHeight) style:UITableViewStylePlain];
    _tableView.backgroundColor = kWhiteColor;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    _commentTable = [[UITableView alloc] initWithFrame:CGRectMake(0, Main_Screen_Height - _bottomHeight, Main_Screen_Width, Main_Screen_Height - _bottomHeight * 2 - 20) style:UITableViewStylePlain];
    _commentTable.backgroundColor = kWhiteColor;
    _commentTable.delegate = self;
    _commentTable.dataSource = self;
    _commentTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_bigView];
    [_bigView addSubview:_tableView];
    [_bigView addSubview:_commentTable];
    
}

- (void)setHeaderAndFooterView{
    
    //添加头部和尾部视图
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Width)];
    headerView.backgroundColor = kWhiteColor;
    
    _tempScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Width)];
    _tempScrollView.contentSize = CGSizeMake(Main_Screen_Width * 2, 0);
    _tempScrollView.pagingEnabled = YES;
    _tempScrollView.showsHorizontalScrollIndicator = NO;
    [headerView addSubview:_tempScrollView];
    
    _headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Width)];
    _headerImageView.contentMode = UIViewContentModeScaleAspectFit;
    _headerImageView.image = [UIImage imageNamed:@"timg.jpeg"];
    [_tempScrollView addSubview:_headerImageView];

    NSArray *imageNames = @[@"j1.jpg",
                            @"j2.jpg",
                            @"j3.jpg",
                            @"j4.jpg",
                            @"j5.jpg" // 本地图片请填写全名
                            ];
    SDCycleScrollView *sdImage = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(Main_Screen_Width, 0, Main_Screen_Width, Main_Screen_Width) shouldInfiniteLoop:YES imageNamesGroup:imageNames];
    sdImage.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    sdImage.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    sdImage.autoScroll = NO;
    sdImage.infiniteLoop = NO;
    [_tempScrollView addSubview:sdImage];

    UIButton *viedoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    viedoBtn.frame = CGRectMake((SCREEN_WIDTH - 110) / 2, _tempScrollView.mj_h - 60, 50, 20);
    [viedoBtn setTitle:@"视频" forState:UIControlStateNormal];
    viedoBtn.backgroundColor = COLOR_GREEN;
    
    viedoBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    viedoBtn.layer.cornerRadius = 4;
    [headerView addSubview:viedoBtn];
    
    UIButton *photoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    photoBtn.frame = CGRectMake(CGRectGetMaxX(viedoBtn.frame) + 10, _tempScrollView.mj_h - 60, 50, 20);
    [photoBtn setTitle:@"图片" forState:UIControlStateNormal];
//    [photoBtn setTitleColor:COLOR_GREEN forState:UIControlStateNormal];
    photoBtn.layer.cornerRadius = 4;
    photoBtn.layer.borderColor = COLOR_GREEN.CGColor;
    photoBtn.layer.borderWidth = 1;
    photoBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    photoBtn.layer.cornerRadius = 4;
    [headerView addSubview:photoBtn];
    
    _tableView.tableHeaderView = headerView;
    UILabel *pullMsgView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 40)];
    pullMsgView.textAlignment = NSTextAlignmentCenter;
    pullMsgView.text = @"商品详情";
    pullMsgView.textColor = KGrayTextColor;

    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 40)];
    [footView addSubview:pullMsgView];

    _tableView.tableFooterView = footView;
    
    //设置下拉提示视图
    UILabel *downPullMsgView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 40)];
    downPullMsgView.textAlignment = NSTextAlignmentCenter;
    downPullMsgView.text = @"下拉显示列表";
    downPullMsgView.textColor = KGrayTextColor;
    
    UIView *downMsgView = [[UIView alloc] initWithFrame:CGRectMake(0, -40, Main_Screen_Width, 40)];
    [downMsgView addSubview:downPullMsgView];
    _commentTable.tableHeaderView = downMsgView;
}

- (void)setBottomView
{
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, Main_Screen_Height - _bottomHeight, Main_Screen_Width, _bottomHeight)];
    bottomView.backgroundColor = KBackgroundColor;
    [self.view addSubview:bottomView];
    
    UIButton *addButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    addButton.frame = CGRectMake(bottomView.mj_w/2, 0, bottomView.mj_w/2, _bottomHeight);
    addButton.backgroundColor = kRedColor;
    addButton.titleLabel.font = SYSTEMFONT(16);
    [addButton setTitle:@"加入购物车" forState:(UIControlStateNormal)];
    [addButton setTitleColor:kWhiteColor forState:(UIControlStateNormal)];
    [addButton addTarget:self action:@selector(addAction) forControlEvents:(UIControlEventTouchUpInside)];
    [bottomView addSubview:addButton];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offset = scrollView.contentOffset.y;

    if (scrollView == _tableView){
        //重新赋值，就不会有用力拖拽时的回弹
        _tempScrollView.contentOffset = CGPointMake(_tempScrollView.contentOffset.x, 0);
        if (offset >= 0 && offset <= Main_Screen_Width) {
            //因为tempScrollView是放在tableView上的，tableView向上速度为1，实际上tempScrollView的速度也是1，此处往反方向走1/2的速度，相当于tableView还是正向在走1/2，这样就形成了视觉差！
            _tempScrollView.contentOffset = CGPointMake(_tempScrollView.contentOffset.x, - offset / 2.0f);
        }
    }

    _scrollViewDidScroll(scrollView);
}

#pragma mark -- 监听滚动实现商品详情与图文详情界面的切换
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    WeakSelf(self);
    CGFloat offset = scrollView.contentOffset.y;
    if (scrollView == _tableView) {
        if (offset > _tableView.contentSize.height - Main_Screen_Height + self.bottomHeight + 50) {
            [UIView animateWithDuration:0.4 animations:^{
                weakself.bigView.transform = CGAffineTransformTranslate(weakself.bigView.transform, 0, -Main_Screen_Height +  self.bottomHeight + TopHeight);
            } completion:^(BOOL finished) {

                self.segm(1);
            }];
        }
    }
    if (scrollView == _commentTable) {
        if (offset < -50) {
            [UIView animateWithDuration:0.4 animations:^{
                [UIView animateWithDuration:0.4 animations:^{
                    weakself.bigView.transform = CGAffineTransformIdentity;

                } completion:^(BOOL finished) {
                    self.segm(0);
                }];
            } completion:^(BOOL finished) {

            }];
        }
    }
}

#pragma mark - action

- (void)addAction
{
    if (_addActionWithBlock) {
        _addActionWithBlock();
    }
}

#pragma mark -- UITableViewDelegate & dataSource

//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//
//    return 1;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _tableView) {
        return 5;
    } else {
        return 5;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == _tableView) {
        if (indexPath.row == 4) {
            return 830;
        } else {
            return 30;
        }
    } else {
        return 200;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == _tableView) {
        static NSString *identifier = @"identifier";
        if (indexPath.row != 4) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            if (indexPath.row == 0) {
                
                cell.textLabel.textColor = KDarkTextColor;
                [cell.textLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
                cell.textLabel.text = @"新鲜夏季酸草莓水果1000g 烘焙蛋糕装饰 冰饮专用";
                
            } else if (indexPath.row == 1) {
                
                cell.textLabel.font = SYSTEMFONT(14);
                cell.textLabel.textColor = [UIColor colorWithHex:0x666666];
                cell.textLabel.text = @"快递：00               销量：2645";
                
            } else if (indexPath.row == 2) {
                
                cell.textLabel.font = SYSTEMFONT(16);
                cell.textLabel.textColor = [UIColor colorWithHex:0xFF9E05];
                cell.textLabel.text = @"￥98.80";
                
            } else if (indexPath.row == 3){
                
                NSString *str = @"优惠     满200减50        购买可获得44积分";
                cell.textLabel.font = SYSTEMFONT(14);
                cell.textLabel.textColor = [UIColor colorWithHex:0x29B566];
                cell.textLabel.text = str;
                NSMutableAttributedString *fontAttributeNameStr = [[NSMutableAttributedString alloc]initWithString:str];
                [fontAttributeNameStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:16] range:NSMakeRange(0, 2)];
                [fontAttributeNameStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, 2)];
                cell.textLabel.attributedText = fontAttributeNameStr;
            }
            return cell;
        } else {
            ProductDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            if (!cell) {
                cell= (ProductDetailTableViewCell *)[[[NSBundle  mainBundle]  loadNibNamed:@"ProductDetailTableViewCell" owner:self options:nil]  lastObject];
            }
            return cell;
        }
    } else {
        ProductCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ProductCommentTableViewCell"];
        if (!cell) {
            cell= (ProductCommentTableViewCell *)[[[NSBundle  mainBundle]  loadNibNamed:@"ProductCommentTableViewCell" owner:self options:nil]  lastObject];
        }
        return cell;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

@end
